﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using WPFExcelReport.Helpers;
using System.Data.OracleClient;
using System.IO;
using INTEX.DataBase.Tools;
using System.Linq;
namespace WPFExcelReport
{
    public partial class YCAEK03DOCReport
    {


        private void FillInSend(DataRow row)
        {
            try
            {
                string value = "";
                Dictionary<string, int> colorpos = new Dictionary<string, int>();
                int x = 1, y = 1;
                x = 5; y = 18;
                //exAgent.SetCellsPicture(5, 17, @"D:\DS40\A1.jpg"); 
                value = row["EKKAUFKRS"].ToString() + "-" + Helper.GetSubRollNum(row["EKKAUFLFD"].ToString());
                exAgent.SetCellValue(3, 5, value);
                exAgent.SetCellValue(2, 5, date.Substring(4, 2) + "月" + date.Substring(6, 2) + "日");
                exAgent.SetCellValue(2, 11, app.User);
                string liefnr = row["EKKLIEFNR"].ToString();
                var qy = from DataRow rx in dsSource.Tables["LIEFDAT"].AsEnumerable()
                         where rx["LSLIEFNR"].ToString() == liefnr
                         select rx;

                foreach (DataRow rx in qy)
                {
                    exAgent.SetCellValue(2, 8, GetGB2312Value(rx["LSNAME1"].ToString().Trim()));
                }
                int rown = FillInIssue();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private int FillInIssue()
        {
            int x = 1, y = 1;
            string value = "";
            int rown = 1;
            string artdes = "", colordes = "";
            string ABPART = "";
            int idx = 0;
            int xStart = 5;
            DataRow dwABAUFM = dsSource.Tables["ICC_ABAUFM"].Rows[0];
            var qek = from DataRow rek in dsSource.Tables["ICC_EKPOS"].AsEnumerable()
                      orderby rek["EKPAUFPOS"].ToString() ascending
                      select rek;
            foreach (DataRow dwEKPOS in qek)
            {
                DataRow dwArticle = null;
                DataRow dwColor = null;
                string exart = "";
                int pos = System.Convert.ToInt16(dwEKPOS["EKPAUFPOS"].ToString());
                string artg = dwEKPOS["EKPARTGR"].ToString();
                string art = dwEKPOS["EKPART"].ToString();
                string var = dwEKPOS["EKPVAR"].ToString();
                var = Left(var, 2);
                string structure = "";
                string composition = "";
                string excolor = "";
                string color = dwEKPOS["EKPFARBE"].ToString();
                string size = dwEKPOS["EKPGROE"].ToString();
                string cref = dwEKPOS["EKPLSGRPBEZ"].ToString().Trim();
                string um = dwEKPOS["EKPMEEH"].ToString();
                double qty = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPME1"].ToString()) * 0.01, 2);
                double tvalue = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPWE1"].ToString()) * 0.01, 2);
                double price = System.Math.Round(System.Convert.ToDouble(dwEKPOS["EKPPRME"].ToString()) * 0.001, 2);
                double gsqm = 1;
                double width = 1;

                var query = from rows in dsSource.Tables["FSSTAMM"].AsEnumerable()
                            where rows["FSART"].ToString() == art
                            select rows;
                switch (artg)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        query = from rows in dsSource.Tables["WASTAMM"].AsEnumerable()
                                where rows["WAART"].ToString() == art
                                & rows["WAVAR"].ToString() == var
                                select rows;
                        foreach (DataRow rw in query)
                        {
                            dwArticle = rw;
                            artdes = GetGB2312Value(rw["WABEZ"].ToString());
                            exart = GetGB2312Value(dwArticle["WALOHNA"].ToString().Trim());
                            width = System.Math.Round(System.Convert.ToDouble(dwArticle["WAFBRTL"].ToString()) * 0.1, 0);
                            gsqm = System.Math.Round(System.Convert.ToDouble(dwArticle["WAFGRQM"].ToString()), 0);
                            structure = //System.Math.Round(System.Convert.ToDouble(dwArticle["WAFFDDIK"].ToString()) * 0.01, 0)
                                //+ "X" + System.Math.Round(System.Convert.ToDouble(dwArticle["WAFFDDIS"].ToString()) * 0.01, 0)
                             "幅宽:" + width + "cm"
                            + "," + gsqm + "g/sqm"
                            ;

                        }
                        composition = Helper.GetComposition(art, dsSource.Tables["STUELI"]);
                        query = from rows in dsSource.Tables["WAFARBEN"].AsEnumerable()
                                where rows["WAFART"].ToString() == art
                                & rows["WAFVAR"].ToString() == var
                               & rows["WAFZUSATZ"].ToString() == color
                                select rows;
                        foreach (DataRow rw in query)
                        {
                            dwColor = rw;
                            colordes = GetGB2312Value(rw["WAFBEZ"].ToString());
                            excolor = GetGB2312Value(rw["WAFMUDESS"].ToString());
                        }
                        break;

                    case "4":
                        break;
                    case "5":
                        query = from rows in dsSource.Tables["FSSTAMM"].AsEnumerable()
                                where rows["FSART"].ToString() == art
                                & rows["FSVAR"].ToString() == var
                                select rows;
                        foreach (DataRow rw in query)
                        {
                            dwArticle = rw;
                            artdes = GetGB2312Value(rw["FSABEZ"].ToString());
                        }
                        query = from rows in dsSource.Tables["FSFARBEN"].AsEnumerable()
                                where rows["FSFART"].ToString() == art
                                & rows["FSFVAR"].ToString() == var
                               & rows["FSFFARBE"].ToString() == color
                                select rows;
                        foreach (DataRow rw in query)
                        {
                            dwColor = rw;
                            colordes = GetGB2312Value(rw["FSFFBEZ"].ToString());
                        }
                        break;
                    case "6":
                        break;
                    case "7":
                        break;
                    case "8":
                        query = from rows in dsSource.Tables["MSSTAMM"].AsEnumerable()
                                where rows["MSMAT"].ToString() == Left(art, 14)
                                & rows["MSVAR"].ToString() == var
                                select rows;
                        foreach (DataRow rw in query)
                        {
                            dwArticle = rw;
                            artdes = GetGB2312Value(rw["MSBEZ"].ToString());
                        }
                        break;
                    default:
                        break;
                } 
         
                exAgent.SetCellValue(xStart + idx * rown, 1, art.Trim() + color.Trim());
                exAgent.SetCellValue(xStart + idx * rown, 2, artdes.Trim());
                exAgent.SetCellValue(xStart + idx * rown, 4, exart);
                exAgent.SetCellValue(xStart + idx * rown, 5, excolor);
                exAgent.SetCellValue(xStart + idx * rown, 8, um);
                exAgent.SetCellValue(xStart + idx * rown, 11, gsqm);
         
                idx++;
                var qMU = from DataRow rw in dsSource.Tables["ABPOS"].AsEnumerable()
                          where rw["ABPAUFKRS"].ToString() == dwABAUFM["ABAVKAUFKRS"].ToString()
                          && rw["ABPAUFLFD"].ToString() == dwABAUFM["ABAVKAUFLFD"].ToString()
                          select rw;
                foreach (DataRow dwABA in qMU)
                {
                    exAgent.SetCellValue(2, 2, GetGB2312Value(dwABA["LSNAME1"].ToString().Trim()));
                    exAgent.SetCellValue(3, 2, GetGB2312Value(dwABA["ABPART"].ToString().Trim()));
                    exAgent.SetCellValue(3, 7, GetGB2312Value(dwABA["FSABEZ"].ToString().Trim()));
                    exAgent.SetCellValue(3, 11, dwABA["ABPAUFKRS"].ToString() + "-" + Helper.GetSubRollNum(dwABA["ABPAUFLFD"].ToString()));

                    break;
                }
            }
            return rown;
        }
    }
}